package uwosh.cs342.project1;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter{
	
	private ArrayList<String> groupList;
	private ArrayList<ArrayList<Contact>> contactList;
	private Context context;
	
	/*	The ExpandableListAdapter constructor is given a group list and a list of contact lists
	 * 	which will be stored in the adapter for editing and display of the stored contacts and
	 * 	groups. The context of the list is set as well.	*/
	public ExpandableListAdapter(Context context, ArrayList<String> groupList, ArrayList<ArrayList<Contact>> contactList){
		this.context = context;
		this.groupList = groupList;
		this.contactList = contactList;
	}//end constructor

	/*	Returns the child (contact) stored in the given position in the contact list.	*/
	public Object getChild(int groupPosition, int childPosition) {
		return contactList.get(groupPosition).get(childPosition);
	}//end getChild

	/*	Returns the id of the contact in the given list position.	*/
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}//end getChildId

	/*	The getChildView method determines what will be displayed for the children
	 * 	(contacts) of each group. For now, all we want is to display the name of the
	 * 	given contact in the list.	*/
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		Contact contact = (Contact) getChild(groupPosition, childPosition);
		if (convertView == null){
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.child_layout, null);
		}
		TextView textView = (TextView) convertView.findViewById(R.id.tvChild);
		textView.setText(contact.toString());
		return convertView;
	}//end getChildView

	/*	Returns the number of children (contacts) a group has at a point in time.	*/
	public int getChildrenCount(int groupPosition) {
		return contactList.get(groupPosition).size();
	}//end getChildren

	/*	Returns the group specified by the given group position.	*/
	public Object getGroup(int groupPosition) {
		return groupList.get(groupPosition);
	}//end getGroup
	
	/*returns the name of the group given the id */
	public String getGroupName (int groupPos){
		return groupList.get(groupPos);
	}

	/*	Returns the number of groups currently being stored.	*/
	public int getGroupCount() {
		return groupList.size();
	}//end getGroupCount

	/*	Returns the id of the given group (the group's position in the list).*/
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}//end getGroupId

	/*	The getGroupView method determines what is displayed for each group in the
	 * 	group list. For now, all we want is to show the name of the group.	*/
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		String group = (String)getGroup(groupPosition);
		if (convertView == null){
			LayoutInflater infalInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.group_layout, null);
		}
		TextView textView = (TextView)convertView.findViewById(R.id.tvGroup);
		textView.setText(group);
		return convertView;
	}//end getGroupView

	/*	Tells a caller whether the adapter has stable ids or not.	*/
	public boolean hasStableIds() {
		return true;
	}//end hasStableIds

	/*	Tells a caller if the given child(contact) is selectable. For now we return
	 * 	true, but in the future we could make certain contacts unselectable, depending
	 * 	on the functionality we want to provide.	*/
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}//end isChildSelectable
	
	/*	The addGroup method adds a new group to the group list, and a new array list of
	 *	contacts is added to correspond to it.	*/
	public void addGroup(String newGroup){
		groupList.add(newGroup);
		contactList.add(new ArrayList<Contact>());
	}//end addGroup
	
	/*	The addContact method adds a new contact to the given group.	*/
	public void addContact(int groupPosition, Contact newContact){
		contactList.get(groupPosition).add(newContact);
	}//end addContact
	
	/*	The getAllGroups method should return a String array of all groups in the list.*/
	public ArrayList<String> getAllGroups(){
		return groupList;
	}//end getAllGroups
	
	public void editContact(Contact newContact, int groupPosition, int childPosition){
		contactList.get(groupPosition).remove(childPosition);
		contactList.get(groupPosition).add(newContact);
	}
	
	public void deleteGroup(int groupPosition){
		groupList.remove(groupPosition);
		contactList.remove(groupPosition);
	}
	
	public void deleteContact(int groupPosition, int childPosition){
		contactList.get(groupPosition).remove(childPosition);
	}
}//end class
